<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="链接" align="center"></el-table-column>
                <el-table-column prop="roleKeywordList" label="权限点代码" align="center"></el-table-column>
                <el-table-column prop="path" label="排序号" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <!--为分页条组件绑定current-change事件，此事件是分页条组件自己定义的事件，当页码改变时触发，对
                    应的处理函数为handleCurrentChang-->

                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <template>
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="上级菜单">
                                                <el-input v-model="formData.parentMenuId"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单链接">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单标识">
                                                <el-input v-model="formData.priority" placeholder="第几个子菜单"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="上传图片">
                                                <el-upload
                                                        class="avatar-uploader"
                                                        action="/menu/upload.do"
                                                        :auto-upload="autoUpload"
                                                        name="imgFile"
                                                        :show-file-list="false"
                                                        :on-success="handleAvatarSuccess"
                                                        :before-upload="beforeAvatarUpload">
                                                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                </el-upload>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="是否禁用">
                                                    <el-radio v-model="formData.disable" label="0">禁用</el-radio>
                                                    <el-radio v-model="formData.disable" label="1">不禁用</el-radio>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-form label-position="right" label-width="100px">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="菜单名称">
                                        <el-input v-model="formData.name"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="上级菜单">
                                        <el-input v-model="formData.parentMenuId"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="菜单链接">
                                        <el-input v-model="formData.linkUrl"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="菜单标识">
                                        <el-input v-model="formData.priority" placeholder="第几个子菜单"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="上传图片">
                                        <el-upload
                                                class="avatar-uploader"
                                                action="/menu/upload.do"
                                                :auto-upload="autoUpload"
                                                name="imgFile"
                                                :show-file-list="false"
                                                :on-success="handleAvatarSuccess"
                                                :before-upload="beforeAvatarUpload">
                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                        </el-upload>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="是否禁用">
                                        <el-radio v-model="formData.disable" label="0">禁用</el-radio>
                                        <el-radio v-model="formData.disable" label="1">不禁用</el-radio>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            autoUpload:true,//自动上传
            activeName:'first',//添加/编辑窗口Tab标签名称
            imageUrl:null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:10,
                total:100
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData:[],//新增和编辑表单中对应的检查项列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit:false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
                name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //权限不足提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403')
                { //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return; }
            },
            handleDelete(row){
                //删除
                this.$confirm('此操作将永久删除该菜单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //用户点击确认以后我发axios带着row.id过去

                    axios.get("/menu/delete.do?id="+row.id).then((res)=>{
                        //删除成功了 返回过来 一个结果类
                        /*
                           private boolean flag;//执行结果，true为执行成功 false为执行失败
                           private String message;//返回结果信息，主要用于页面提示信息
                           private Object data;//返回数据
                        */
                        if (res.data.flag){
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                            this.findPage();
                        }else {
                            this.$message.error(res.data.message)
                        }
                    }).finally(()=>{
                        this.findPage();
                    }).catch((r)=>{
                        this.showMessage(r)
                    })

                }).catch((r)=>{
                    this.showMessage(r)
                })
            },
            handleEdit(){
                this.dialogFormVisible4Edit=false;
                axios.post("/menu/editMenu.do",this.formData).then((res)=>{
                    this.$message({
                        message:res.data.message,
                        type:res.data.flag?'success':'error'
                    })
                }).finally((res)=>{

                    this.findPage();
                    this.resetForm();
                })
            },
            //编制按钮
            handleUpdate(row){
                this.resetForm();
                axios.get("/menu/findMenuByID.do?id="+row.id).then((res)=>{
                    if (res.data.flag){
                    this.formData=res.data.data;
                this.imageUrl="http://q1jygupxd.bkt.clouddn.com/"+res.data.data.icon;
                     this.dialogFormVisible4Edit=true;
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch((r)=>{
                    this.showMessage(r)
                })
            },
            //添加
            handleAdd () {

                axios.post("/menu/add.do",this.formData).then((res)=>{
                    this.$message({
                        type: res.data.flag? 'success':'error',
                        message:res.data.message
                    })
                    this.dialogFormVisible=false;
                    this.findPage();
                }).catch((r)=>{
                    this.showMessage(r)
                })

            },
            // 重置表单
            resetForm() {
                this.formData={};
                this.imageUrl = null;
            },
            // 弹出添加窗口
            handleCreate() {
                //清空表单数据
                this.resetForm();
                //弹出添加窗口
                this.dialogFormVisible=true;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传菜单图标只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传菜单图标大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                //    imageUrl:null,//模型数据，用于上传图片完成后图片预览
                //图片在七牛云只要给个地址 就能看到图片
                this.imageUrl="http://q1jygupxd.bkt.clouddn.com/"+response.data;
                this.$message({
                    message:response.message,
                    type:response.flag?'success':'error'
                })
                //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                //此时这个表单里面有图片了 ,我们应该连同图片信息上传到服务器
                this.formData.icon=response.data;
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();
            },
            findPage(){
                var param={
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize
                };
                axios.post("/menu/findpage.do",param).then((res)=>{
                    //res为后台回写过来的数据json对象
                    // res.data.rows 是拿到返回的数据的所有内容
                    if (res.data.total==0){
                        this.$message.error("未找到相关菜单项");
                    }
                    this.tableData=res.data.rows;//将所有内容给双向绑定的dataList变量
                    //查询到的总页数给前台展示
                    //为什么不需要把当前页和每页显示的条数给pagination?因为这两个是前端传给后台的数据,而总页数为后台给前台的
                    this.pagination.total=res.data.total;
                }).catch((r)=>{
                    this.showMessage(r)
                })
            }, //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage=currentPage;
                this.findPage();
            },
            //分页查询
            findPageByInfo(){
                this.pagination.currentPage=1;
                this.findPage();
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    })
</script>
</body>
</html>